<!-- 收据管理窗体 -->
<template>
  <el-dialog
    v-model="dialogVisible"
    title="收据管理"
    width="1200"
    overflow
    @close="handleClose"
  >
    <div style="padding: 10px 20px">
      <el-form label-width="120px" style="margin-top: 2%">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称">
              <el-input
                v-model="form.projectName"
                placeholder="请输入 项目名称"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同名称">
              <el-input
                v-model="form.contractName"
                placeholder="请输入 合同名称"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商">
              <el-input
                v-model="form.supplierDeptName"
                placeholder="请输入 供应商"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="预收款截止日期" style="width: 85%">
              <el-input
                v-model="form.contractPrePaymentDate"
                placeholder="请输入 预收款截止日期"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合同预收款" style="width: 85%">
              <div style="display: flex">
                <el-input
                  v-model="form.contractPrePaymentRate"
                  placeholder="请输入 合同预收款"
                  clearable
                  disabled
                />
                <div style="margin-left: 20px">%</div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预收款金额" style="width: 85%">
              <div style="display: flex">
                <el-input
                  v-model="form.contractPrePaymentPrice"
                  placeholder="请输入 预收款金额"
                  clearable
                  disabled
                />
                <div style="margin-left: 20px">元</div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="缴费单位或个人">
              <el-input
                v-model="form.receiptUnitName"
                placeholder="请输入 缴费单位或个人"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-tabs v-model="activeName" class="demo-tabs">
          <el-tab-pane label="申请收据" name="申请收据">
            <el-form label-width="120px" style="margin-top: 2%">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="申请人">
                    <el-input
                      v-model="form.applyUserName"
                      placeholder="请输入 申请人"
                      clearable
                      disabled
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请时间">
                    <el-date-picker
                      v-model="form.applyDate"
                      type="date"
                      value-format="YYYY-MM-DD"
                      disabled
                      placeholder=""
                      style="width: 100%"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="申请说明">
                    <el-input
                      v-model="form.applyMark"
                      placeholder="请输入 申请说明"
                      clearable
                      :rows="3"
                      type="textarea"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="receiptsaveOrUpd">
          提交申请
        </el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { reactive, ref, watch, onMounted } from "vue";
import type { TabsPaneContext } from "element-plus";
import { getreceiptsaveOrUpd } from "@/api/ERP_PaymentMange";
import { ElMessage, ElMessageBox } from "element-plus";
import { useGlobal, isAllEmpty } from "@pureadmin/utils";
import { useUserStoreHook } from "@/store/modules/user";
// ------------------- 公共 -------------------
// 弹窗关闭/打开
const props = defineProps({
  visible: Boolean,
  date: Object
});
const dialogVisible = ref(false);
const emit = defineEmits(["close"]);
function handleClose() {
  emit("close", false);
}
watch(
  () => props.visible,
  val => {
    dialogVisible.value = val;
    if (props.date) {
      (form.projectName = props.date?.projectName),
        (form.contractName = props.date?.contractName),
        (form.supplierDeptName = props.date?.supplierDeptName),
        (form.contractPrePaymentDate = props.date?.contractPrePaymentDate),
        (form.contractPrePaymentRate = props.date?.contractPrePaymentRate),
        (form.contractPrePaymentPrice = props.date?.contractPrePaymentPrice),
        (form.receiptUnitName = ""),
        (form.applyUserName = isAllEmpty(useUserStoreHook()?.nickname)
          ? useUserStoreHook()?.username
          : useUserStoreHook()?.nickname),
        (form.applyDate = getCurrentDateInFormat()),
        (form.applyMark = "");
    }
  },
  { immediate: true }
);
//---------------------------------
let form = reactive({
  projectName: props.date?.projectName,
  contractName: props.date?.contractName,
  supplierDeptName: props.date?.supplierDeptName,
  contractPrePaymentDate: props.date?.contractPrePaymentDate,
  contractPrePaymentRate: props.date?.contractPrePaymentRate,
  contractPrePaymentPrice: props.date?.contractPrePaymentPrice,
  receiptUnitName: "",
  applyUserName: isAllEmpty(useUserStoreHook()?.nickname)
    ? useUserStoreHook()?.username
    : useUserStoreHook()?.nickname,
  applyDate: getCurrentDateInFormat(),
  applyMark: ""
});
const activeName = ref("申请收据");

async function receiptsaveOrUpd() {
  if (form.receiptUnitName == "") {
    return ElMessage("请先填写缴费单位或个人");
  }
  let params: any = {};
  params.id = props.date?.id;
  params.receiptUnitName = form.receiptUnitName;
  params.applyDate = form.applyDate;
  params.applyMark = form.applyMark;
  params.status = props.date?.status;
  params.bizType = "so";
  getreceiptsaveOrUpd(params).then((response: any) => {
    if (response.code === 200 && response.data) {
      ElMessage("提交成功");
      emit("close", false);
    }
  });
}

function getCurrentDateInFormat() {
  var now = new Date(); // 获取当前日期和时间
  var year = now.getFullYear(); // 获取年份
  var month = ("0" + (now.getMonth() + 1)).slice(-2); // 获取月份，从0开始计数，所以加1
  var day = ("0" + now.getDate()).slice(-2); // 获取日期

  // 拼接成 yyyyMMdd 的格式
  return year + "-" + month + "-" + day;
}
</script>
